// pages/newInformationthree/newInformationthree.js
//获取应用实例
const app = getApp()
wx.cloud.init()
Page({

   /**
    * 页面的初始数据
    */
   data: {
      titleImg: '/images/own/logo.jpg',
      userName: '',
      password: '',
      confirmPassword: '',
      placeholderInput: '请输入账号',
      disabled: false,
      loginBtnName: "绑定",
      placeholder: "确认密码",
      msg:""
   },
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
      // console.log(options.titleImg);
      this.setData({titleImg:options.titleImg})
      // console.log(app.globalData.token);
      wx: wx.request({
         url: app.globalData.myUrl + '/userByWeb/getUsername',

         method: "GET",
         data: {
            "token": app.globalData.token
         },
         
         success: (result) => {
            // console.log(result.data);
            // 判断是否成功获取用户名
            if (result.data.msg===null) {
               wx.showToast({
                  title: '您还没有绑定喔',
                  icon: 'none',
                  duration: 1000
               })
               this.setData({
                  msg:result.data.msg
               })
               return;
            } else {
               //   直接获取用户名 
               // console.log(2);
               this.setData({
                  userName: result.data.msg,
                  placeholderInput: result.data.msg,
                  disabled: true,
                  loginBtnName: "修改",
                  placeholder: "修改密码"
               })

            }
         },
         fail: (res) => {
         }
      })
   },
   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function () {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function () {},

   // 账号
   usernameInput(e) {
      let userName = e.detail.value;
      // console.log(e);
      if (!(/^[a-zA-Z][a-zA-Z0-9_]{4,11}/.test(userName))) {
         wx.showToast({
            title: '字母开头，不能为空格',
            duration: 500,
            icon: 'none'
         });
         return;
      } else {
         this.setData({
            // 清除空格符  变为空
            userName: userName,
            userName: userName.replace(/\s+/g, ''),
         })
         // console.log(this.data.userName);
      }

      // 正则使用

      // console.log(1);
   },
   // 密码
   passwordInput(e) {
      // console.log(e);
      let password = e.detail.value;
      // 正则
      if (!(/^[a-zA-Z]\w{5,17}$/.test(password))) {
         wx.showToast({
            title: '字母开头,长度5~17位',
            duration: 500,
            icon: 'none'
         });
         return
      };
      this.setData({
         password: password,
         password: password.replace(/\s+/g, ''),
      })
      // console.log(this.data.password);
   },
   // 确认密码
   confirmPassword(e) {
      // console.log(e);
      let confirmPassword = e.detail.value;
      if (!(this.data.password == confirmPassword)) {
         wx.showToast({
            title: '密码不一致',
            duration: 500,
            icon: 'none'
         });
         return;
      }
      this.setData({
         confirmPassword: confirmPassword,
         confirmPassword: confirmPassword.replace(/\s+/g, ''),
      })
      // console.log(confirmPassword);

   },
   // 重置
   resetSalaryBtn(e) {
      this.setData({
         userName: '',
         password: '',
         confirmPassword: ''
      })
   },

   // 登录处理
   login() {
      let userName=this.data.userName;
      let password=this.data.password;
      var that = this;
      if (this.data.userName.length == 0 || this.data.password.length == 0) {
         wx.showToast({
            title: '账号或密码不能为空',
            icon: 'none',
            duration: 1000
         })
         return;
      } 
      if (this.data.msg===null) {
         // msg为空 就绑定账户
         // url: app.globalData.myUrl + '/userByWeb/getUsername',
         wx:wx.request({
            url:app.globalData.myUrl + '/userByWeb/register',
            method:"POST",
            header: {"Content-Type":"application/x-www-form-urlencoded"},
            data:{
               "token": app.globalData.token,
               username:userName,
               password:password
            },
            // Howe10086
            success:(res)=>{
               wx.showToast({
                  title: '绑定成功',
                  icon: 'none',
                  duration: 1000
               }),
               this.setData({
                  userName: res.data.msg,
                  placeholderInput: res.data.msg,
                  disabled: true,
                  loginBtnName: "修改",
                  placeholder: "修改密码"
               })
            },
            fail:()=>{
               wx.showToast({
                  title: '绑定失败',
                  icon: 'none',
                  duration: 1000
               })
            }
         })
         
      }else{
         // 否则就是修改
         wx:wx.request({
            url:app.globalData.myUrl + '/userByWeb/update',
            method:"POST",
            header: {"Content-Type":"application/x-www-form-urlencoded"},
            data:{
               "token": app.globalData.token,
               username:userName,
               password:password
            },
            // Howe10086
            success:(res)=>{
               wx.showToast({
                  title: '修改成功',
                  icon: 'none',
                  duration: 1000
               })
            },
            fail:()=>{
               wx.showToast({
                  title: '修改失败',
                  icon: 'none',
                  duration: 1000
               })
            }
         })
      }
   },


   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function () {},

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function () {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function () {

   },

   /**
   * 分享到朋友圈
   */
  // onShareTimeline: function() {
  //   return {
  //     title: '一个小demo',
  //     imageUrl: "https://www.kongbai121.xyz/drawing-bed/2023-01/59e40761-c197-4b17-be36-bfbd1f2cfa1c.jpg"
  //   }
  // },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '一个小demo',
      path: '/pages/loading/loading',
      imageUrl: "https://www.kongbai121.xyz/drawing-bed/2023-01/59e40761-c197-4b17-be36-bfbd1f2cfa1c.jpg"
    }
  }



})